﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using StuManModels;
using StuManDAL.Common;
using System.Data;

namespace StuManDAL
{
    public class ClassService
    {
        public List<StudentClass> GetAllClass(out string info)
        {
            string sql = "select ClassId,ClassName from StudentClass";
            try
            {
                SqlDataReader reader = SQLHelper.Instance.GetReader(sql);
                List<StudentClass> studentClasses = new List<StudentClass>();
                while (reader.Read())
                {
                    studentClasses.Add(new StudentClass()
                    {
                        ClassId = Convert.ToInt32(reader["ClassId"]),
                        ClassName = reader["ClassName"].ToString()
                    });
                }
                info = "读取完成";
                reader.Close();
                return studentClasses;
            }
            catch (SqlException ex)
            {
                info = "操作数据库出现异常，异常信息为：" + ex.Message;
                return null;
            }
            catch (Exception ex)
            {
                info = ex.Message;
                return null;
            }
        }
        /// <summary>
        /// 获得所有班级并填充至DataSet
        /// </summary>
        /// <param name="info">操作信息</param>
        /// <returns></returns>
        public DataSet GetAllClassForDataSet(out string info)
        {
            string sql = "select ClassId,ClassName from StudentClass";
            try
            {
                info = string.Empty;
                return SQLHelper.Instance.GetDataSet(sql);
            }
            catch (SqlException ex)
            {
                info = "操作数据库出现异常，异常信息为：" + ex.Message;
                return null;
            }
            catch (Exception ex)
            {
                info = ex.Message;
                return null;
            }
        }
    }
}
